/**
 * Created by WYH on 17/4/19.
 */
angular.module('tribalc').factory('TimeFormat', function () {



    function changeTimestampToStr(int,string) {
        var date = new Date(int);

        return date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日";
    }
    
    function changeSecondToStr(second) {
        var hour, minite;
        hour = parseInt(second / 60 / 60) == 0 ? '00' : parseInt(second / 60 / 60);
        minite = parseInt(second / 60 % 60) == 0 ? '00' : parseInt(second / 60 % 60);
         if(minite==59){
            hour =hour+1;
            minite= 0;
        }
        hour = (hour+'').length == 1 ? '0' + hour : hour + '';
        minite = (minite+'').length == 1 ? '0' + minite : minite + '';
        return hour + ':' + minite;
    }
    
    function changeHourTimeStrToInt(timeStr) {
        var matchTimeArr = timeStr.match(/(\d+):(\d+)/);
        if (!matchTimeArr || matchTimeArr.length != 3) {
            return 0;
        } else {
            var hourInt = parseInt(matchTimeArr[1]);
            var minInt = parseInt(matchTimeArr[2]);
            return hourInt * 60 * 60 + minInt * 60;
        }
    }

    function changeYearTimeToInt(str) {
        if (!str) return 0;
        var date = new Date(str.replace(/-/g, '/'));
        return date.getTime();
    }

    function changeIntToYearTime(int,string) {
        var date = new Date(int);
        if(string){
            return date.getFullYear() + string + (date.getMonth() + 1) + string + date.getDate();
        }
        return date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日";
    }

    
    
    return {
        changeTimestampToStr: changeTimestampToStr,
        changeSecondToStr:changeSecondToStr,
        changeStrToInt: changeHourTimeStrToInt,
        changeYearTimeToInt:changeYearTimeToInt,
        changeIntToYearTime: changeIntToYearTime
    }


});